package com.travel.server.manager.impl.dal;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.ProjectGroupDao;
import com.travel.server.manager.model.ProjectGroup;
import com.travel.server.manager.model.ProjectGroupExample;
import com.travel.server.manager.model.ProjectInfo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/26 16:57
 * @Desc:
 */
@Component
public class GroupDal {

    @Autowired
    private ProjectGroupDao projectGroupDao;

    @Autowired
    private ProjectDal projectDal;

    public PageDto<ProjectGroup> select(Integer offset, Integer limit){
        GroupParam param = new GroupParam();
        param.setOffset(offset);
        param.setLimit(limit);
        ProjectGroupExample example = makeExample(param);
        example.setOrderByClause("create_time desc");

        List<ProjectGroup> list = projectGroupDao.selectByExample(example);
        int total = projectGroupDao.countByExample(example);

        PageDto<ProjectGroup> pageDto = new PageDto<>();
        pageDto.setList(list);
        pageDto.setTotal(total);

        return pageDto;
    }

    public List<ProjectGroup> selectEnum(Integer parentClass){
        GroupParam param = new GroupParam();
        if(parentClass != null){
            param.setParentClass(parentClass);
        }
        ProjectGroupExample example = makeExample(param);
        example.setOrderByClause("parent_class asc, number asc");

        return projectGroupDao.selectByExample(example);
    }

    public ProjectGroup selectById(Integer id){
        return projectGroupDao.selectByPrimaryKey(id);
    }

    public void updateOrInsert(ProjectGroup projectGroup){
        if(projectGroup.getId() != null){
            projectGroupDao.updateByPrimaryKey(projectGroup);
        }else{
            projectGroupDao.insertSelective(projectGroup);
        }
    }

    public boolean hasUnCompleteOrder(String projectName, String projectTypeName){
        ProjectDal.ProjectParam param = new ProjectDal.ProjectParam();
        if(StringUtils.isNotBlank(projectName)){
            param.setName(projectName);
        }
        if(StringUtils.isNotBlank(projectTypeName)){
            param.setType(projectTypeName);
        }
        List<ProjectInfo> list = projectDal.selectByExample(param);
        if(!CollectionUtil.isEmpty(list)){
            List<Integer> idList = new ArrayList<>();
            for(ProjectInfo projectInfo : list){
                idList.add(projectInfo.getId());
            }
            return projectDal.hasUnCompleteOrder(idList);
        }
        return false;
    }

    public List<ProjectGroup> selectByExample(GroupParam param){
        ProjectGroupExample example = makeExample(param);
        return projectGroupDao.selectByExample(example);
    }

    private ProjectGroupExample makeExample(GroupParam param){
        ProjectGroupExample example = new ProjectGroupExample();
        ProjectGroupExample.Criteria criteria = example.or();
        if(param.getState() != null) {
            criteria.andStateEqualTo(param.getState());
        }
        if(StringUtils.isNotBlank(param.getName())){
            criteria.andNameEqualTo(param.getName());
        }
        if(!CollectionUtil.isEmpty(param.getIdList())){
            criteria.andIdIn(param.getIdList());
        }
        if(param.getParentClass() != null){
            criteria.andParentClassEqualTo(param.getParentClass());
        }
        if(param.getOffset() != null && param.getLimit() != null) {
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }
        return example;
    }

    @Data
    public static class GroupParam{
        private String name;

        private List<Integer> idList;

        private Integer parentClass;

        private Integer state = 1;

        private Integer offset;

        private Integer limit;
    }
}
